import {useRoute} from "vue-router";
import {onMounted, ref} from "vue";
import {getSubCategoryAPI} from "@/apis/category.js";


export function useTwoGoodList() {
  const route = useRoute()
  const goodList = ref([])
  const disabled = ref(false)
  const reqData = ref({
    categoryId: route.params.id,
    page: 1,
    pageSize: 20,
    sortField: 'publishTime'
  })

  const getGoodList = async () => {
    const res = await getSubCategoryAPI(reqData.value)
    goodList.value = res.result.items
  }

  const getMoreGoodList = async () => {
    reqData.value.page++
    const res = await getSubCategoryAPI(reqData.value)
    goodList.value = [...goodList.value, ...res.result.items]
    if (res.result.items.length === 0) {
      disabled.value = true
    }
  }

  onMounted(() => getGoodList())

  return {
    goodList,
    reqData,
    getGoodList,
    getMoreGoodList,
    disabled
  }
}
